// pages/category/index.js
import {request} from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 左数据
    leftList:[],
    // 右数据
    rightList:[],
    // 被点击的左侧的菜单
    currentIndex:0,
    scrollTop:0
  },
 cates:[], 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   const cates = wx.getStorageSync('cates');
   if(!cates) {
     this.getCates()
   }else {
      if(Date.now()-cates.time>1000*10) {
        this.getCates()
      }else {
        console.log('旧数据')
        this.cates = cates.data
        let leftList = this.cates.map(v => v.cat_name)
        let rightList = this.cates[0].children
        this.setData({
          leftList,
          rightList
        })
      }
   }
  },
  async getCates() {
    // request({ 
    //   url: '/categories'
    // }).then(res => {
    //   this.cates = res.data.message
    //   wx.setStorageSync('cates', {time:Date.now(),data:this.cates})
    //   let leftList = this.cates.map(v => v.cat_name)
    //   let rightList = this.cates[0].children
    //   this.setData({
    //     leftList,
    //     rightList
    //   })
    // })
    const res = await request({url: '/categories'})
    this.cates = res.data.message
    wx.setStorageSync('cates', {time:Date.now(),data:this.cates})
    let leftList = this.cates.map(v => v.cat_name)
    let rightList = this.cates[0].children
    this.setData({
      leftList,
      rightList
    })
  },
  handleItemTap (e) {
    console.log(e)
    const {index} = e.currentTarget.dataset
    let rightList = this.cates[index].children
    this.setData({
      currentIndex:index,
      rightList,
    scrollTop:0
    })
    // 重新设置右侧内容的高度

  }
})